﻿@using Microsoft.JSInterop
@using Covenant.Models.Indicators
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime

<CovenantTable TItem="NetworkIndicator" Items="NetworkIndicators.ToList()" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="NetworkIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.Protocol">
            <HeaderContent>Protocol</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="NetworkIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.Domain">
            <HeaderContent>Domain</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="NetworkIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.IPAddress">
            <HeaderContent>IPAddress</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="NetworkIndicator" TItem2="int" GetSortableProperty="(indicator) => indicator.Port">
            <HeaderContent>Port</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="NetworkIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.URI">
            <HeaderContent>URI</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="indicator">
        <tr>
            <td>@indicator.Protocol</td>
            <td>@indicator.Domain</td>
            <td>@indicator.IPAddress</td>
            <td>@indicator.Port</td>
            <td>@indicator.URI</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<NetworkIndicator> NetworkIndicators { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 25;

    private bool IsRowDisplayed(NetworkIndicator indicator, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            indicator.Protocol.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.Domain.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.IPAddress.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.Port.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.URI.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}
